package cn.demo.um.support.drive;

import cn.demo.um.support.handler.MatcherHandlerWrapper;
import cn.demo.um.support.model.MatchScoreDto;
import cn.demo.um.support.handler.MatcherContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@Slf4j
public class MatcherDrive {
    /*该用户择偶标准对应的处理器*/
    private List<MatcherHandlerWrapper> handlers;
    private MatcherContext matcherContext;

    /**
     * @param start：从第几个开始
     * @param limit：取多少个
     * @return
     */
    public List<MatchScoreDto> invoke(long start,int limit) {
        /*执行用户择偶标准的所有处理器*/
        for (MatcherHandlerWrapper handler : handlers) {
            handler.invoke(matcherContext);
        }
        List<MatchScoreDto> scoreDtos = matcherContext.getMatchScoreDtoMap().values().stream()
                .sorted((o1, o2) -> Double.compare(o2.getScore(), o1.getScore()))
                .skip(start)
                .limit(limit)
                .collect(Collectors.toList());
        return scoreDtos;
    }

    public double totalScore(){
        return handlers.stream().mapToDouble(MatcherHandlerWrapper::getScore).sum();
    }

}
